package com.swak.boot.config.zookeeper.config;

import java.util.List;

import org.springframework.core.env.ConfigurableEnvironment;

import com.swak.Constants;
import com.swak.boot.config.zookeeper.ZookeeperProperties;
import com.swak.utils.Lists;
import com.swak.utils.StringUtils;

import lombok.Getter;

/**
 * 配置文件的数据加载
 * 
 * @author 618lf
 */
public class ConfigDataLoader {

	@Getter
	private List<String> contexts;
	@Getter
	private ZookeeperProperties properties;

	public ConfigDataLoader(ConfigurableEnvironment environment) {
		this.contexts = Lists.newArrayList();
		this.properties = this.parseConfig(environment);
		String name = environment.getProperty("spring.application.name");
		String active = environment.getProperty("spring.profiles.active");
		this.loadSharedConfigs(properties);
		this.loadApplicationConfigs(properties, name, active);
	}

	/**
	 * 加载共享配置
	 */
	private void loadSharedConfigs(ZookeeperProperties properties) {
		String sharedConfigs = properties.getSharedConfigs();
		if (StringUtils.isNotBlank(sharedConfigs)) {
			String[] configs = StringUtils.split(sharedConfigs, ",");
			for (String config : configs) {
				this.loadConig(properties, config);
			}
		}
	}

	/**
	 * 加载应用配置
	 */
	private void loadApplicationConfigs(ZookeeperProperties properties, String name, String active) {
		if (StringUtils.isBlank(name)) {
			return;
		}
		this.loadConig(properties, name + ".properties");
		if (StringUtils.isBlank(active)) {
			return;
		}
		this.loadConig(properties, name + "-" + active + ".properties");
	}

	private void loadConig(ZookeeperProperties properties, String dataId) {
		String path = new StringBuilder().append(Constants.PATH_SEPARATE)
				.append(StringUtils.defaultIfBlank(properties.getNamespace(), "public")).append(Constants.PATH_SEPARATE)
				.append(StringUtils.defaultIfBlank(properties.getGroup(), "default")).append(Constants.PATH_SEPARATE)
				.append(dataId).toString();
		contexts.add(path);
	}

	private ZookeeperProperties parseConfig(ConfigurableEnvironment environment) {
		String address = environment.getProperty("spring.zookeeper.address");
		String username = environment.getProperty("spring.zookeeper.username");
		String password = environment.getProperty("spring.zookeeper.password");
		String timeout = environment.getProperty("spring.zookeeper.connectionTimeoutMs");
		String sessionExpireMs = environment.getProperty("spring.zookeeper.sessionTimeoutMs");
		String namespace = environment.getProperty("spring.zookeeper.namespace");
		String group = environment.getProperty("spring.zookeeper.group");
		String sharedConfigs = environment.getProperty("spring.zookeeper.sharedConfigs");

		ZookeeperProperties properties = new ZookeeperProperties();
		if (StringUtils.isNotBlank(address)) {
			properties.setAddress(address);
		}
		if (StringUtils.isNotBlank(username)) {
			properties.setUsername(username);
		}
		if (StringUtils.isNotBlank(password)) {
			properties.setPassword(password);
		}
		if (StringUtils.isNotBlank(timeout)) {
			properties.setConnectionTimeoutMs(Integer.parseInt(timeout));
		}
		if (StringUtils.isNotBlank(sessionExpireMs)) {
			properties.setSessionTimeoutMs(Integer.parseInt(sessionExpireMs));
		}
		if (StringUtils.isNotBlank(namespace)) {
			properties.setNamespace(namespace);
		}
		if (StringUtils.isNotBlank(group)) {
			properties.setGroup(group);
		}
		if (StringUtils.isNotBlank(sharedConfigs)) {
			properties.setSharedConfigs(sharedConfigs);
		}
		return properties;
	}
}